/*
 * Copyright (c) 1994-2017, Joyoung Stock CO.LTD.
 * The FWJR Project.
 * All Rights Reserved.
 */

package com.hepinghospital.user.ui.dialog;

import android.app.Dialog;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseDialogFragment;
import com.rest.response.PrescriptionResponse;

import java.util.ArrayList;
import java.util.List;

import jz.joyoung.robot.util.DpUtil;


public class BindCardDialog extends BaseDialogFragment {
    private OnItemClickListener mListener;
    private RecyclerView recyclerView;
    private Dialog mDialog;
    private TextView btn_confirm,btn_cancel;

    List<String> list = new ArrayList<>();
    AddMedicineAdapter adapter;
    PrescriptionResponse.Prescription p = new PrescriptionResponse.Prescription();

    public static BindCardDialog newInstance() {
        return new BindCardDialog();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog(savedInstanceState);
        mDialog.setCancelable(false);
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity(Gravity.BOTTOM);
        }
        return mDialog;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mView = inflater.inflate( R.layout.dialog_bind_card, container, false);
        mView.setMinimumWidth(mScreenWidth);

        btn_confirm = mView.findViewById(R.id.btn_confirm);
        btn_cancel = mView.findViewById(R.id.btn_cancel);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView = mView.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(manager);
        adapter = new AddMedicineAdapter();
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = DpUtil.dip2px(getActivity(), 1);
            }
        });

        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.callback();
                dismiss();
            }
        });
        return mView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getDialog().setCanceledOnTouchOutside(false);
    }

    public interface OnItemClickListener {
        void callback();

    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

//    @Override
//    public void onClick(View view) {
//        switch (view.getId()) {
//            case R.id.tv_check: {
//                mListener.check();
//                dismiss();
//            }
//            break;
//            case R.id.tv_refuse: {
//                mListener.refuse();
//                dismiss();
//            }
//            break;
//            case R.id.txt_cancel: {
//                dismiss();
//            }
//            break;
//            default:
//                break;
//        }
//    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }


    class AddMedicineAdapter extends RecyclerView.Adapter<AddMedicineAdapter.Holder> {


        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            Holder myHolder;
            myHolder = new Holder(LayoutInflater.from(getActivity()).inflate(R.layout.item_card, parent, false));
            return myHolder;
        }

        @Override
        public void onBindViewHolder(Holder holder, final int position) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });
//            holder.tv_name.setText(list.get(position).name + " " + list.get(position).drugSpecification);
//            holder.tv_make.setText(list.get(position).manufacturer);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_name, tv_make;
            ImageView img_card;

            public Holder(View itemView) {
                super(itemView);
                img_card = itemView.findViewById(R.id.img_card);
            }
        }
    }






}
